$(function() {
	$("#loading-first").fadeOut(500);
	var relearr = [];
	var removerelearr = [];
	var tishi;
	requestnorelevance();
	requestrelevance();
	var page=1;
	//根据页数进行跳转
	$('.pagebtn').on('click', function() {
		requestnorelevance($('.page-in').val());
		$('.page').html($('.page-in').val());
		page=$('.page-in').val()
	});
	//多个关联
	$('.morerelevance').on('click', function() {
		tishi=layer.open({
			type: 1,
			title: false,
			//具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
				
			id: "delete" //防止重复弹出
				,
			content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否关联</div>",
			btn: ['确定', '取消'],
			btnAlign: 'c' //按钮居中
				,
			shade: 0 //不显示遮罩
				,
			yes: function() {
				layer.close(tishi);
				relearr = []
				for(var i = 0; i < $('.norelevance').find('tbody').find('tr').find('input[type=checkbox]').length; i++) {
					var check = $('.norelevance').find('tbody').find('tr').find('input[type=checkbox]').eq(i).prop('checked')
					if(check) {
						relearr.push($('.norelevance').find('tbody').find('tr').eq(i).data('carid'))
					}
				}
				console.log(relearr)
				proceedrelevance("{:input('param.driver_id')}", relearr)
				requestrelevance();
			},
			btn2: function() {
				layer.closeAll();
			}
		});
	});
	//全选
	$('input[type=checkbox]').on('click', function() {
		checkall(this)
	});
	//上一页
	$('.previous').on('click', function() {
		if($('.page').html() <= 1) {
			$('.page').html(1)
		} else {
			var p = $('.page').html() - 0
			p -= 1;
			page=p;
			$('.page').html(p)
			requestnorelevance(p)
		}
	});
	//下一页
	$('.next').on('click', function() {
		var p = $('.page').html() - 0
		p += 1;
		page=p;
		$('.page').html(p)
		requestnorelevance(p)
	});
	//请求所有车辆
function requestnorelevance(page) {
		var norelevanceload = layer.load(0, {
			shade:  [0.3,'#fff']
		});
		if(page){
			$.ajax({
				type: "post",
				url: "{:url('adminapi/driver/searchCar')}",
				data: {
					page: page,
					pagenum: 10,
					company_id:$('.company_all_id').val()
				},
				success: function(res) {
					layer.close(norelevanceload);
					console.log(res)
					var norelevance = '',nomessage='无数据';
					
					if(res.code == 1) {
						$('.num').html(`共${res.data.count}辆车`);
						for(var i = 0; i < res.data.list.length; i++) {
							norelevance += `<tr data-carid='${res.data.list[i].car_id}' data-typeid='${res.data.list[i].car_type_id}'>
									<td><input type="checkbox" class="check-list"></td>
									<td>${res.data.list[i].number}</td>
									<td>${res.data.list[i].car_type_name}</td>
									<td>`
							if(res.data.list[i].has_bind==1){
								norelevance += `	
										<div class="tpl-table-black-operation">
											已关联
										</div>`
							}else{
							norelevance += `	
										<div class="tpl-table-black-operation">
											<a class='bind'>
												<i class="am-icon-pencil"></i> 关联
											</a>
										</div>`
							}
							norelevance += `		
									</td>
								</tr>`
						}
						$('.norelevance').find('tbody').html(norelevance)
						$('.bind').on('click',function(){
							console.log(1)
							var that=this
							tishi=layer.open({
								type: 1,
								title: false,
								
								id: "delete" //防止重复弹出
									,
								content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否关联</div>",
								btn: ['确定', '取消'],
								btnAlign: 'c' //按钮居中
									,
								shade: 0 //不显示遮罩
									,
								yes: function() {
									relearr=[];
									relearr.push($(that).closest('tr').data('carid'))
									proceedrelevance("{:input('param.driver_id')}",relearr)
									layer.close(tishi);
								},
								btn2: function() {
									layer.closeAll();
								}
							});
							
						})
					}

				}
			});
		}else{
			$.ajax({
				type: "post",
				url: "{:url('adminapi/driver/searchCar')}",
				data: {
					page: 1,
					pagenum: 10,
					company_id:$('.company_all_id').val()
					
					{if $Think.get.number}
					,number:"{$Think.get.number}"
					{/if}
					{if $Think.get.type}
					,type:"{$Think.get.type}"
					{/if}
				},
				success: function(res) {
					layer.close(norelevanceload);
					console.log(res)
					var norelevance = ''
					if(res.code == 1) {
						$('.num').html(`共${res.data.count}辆车`)
						for(var i = 0; i < res.data.list.length; i++) {
							
							norelevance += `<tr data-carid='${res.data.list[i].car_id}' data-typeid='${res.data.list[i].car_type_id}'>
									<td><input type="checkbox" class="check-list"></td>
									<td>${res.data.list[i].number}</td>
									<td>${res.data.list[i].car_type_name}</td>
									<td>`
							if(res.data.list[i].has_bind==1){
								norelevance += `	
										<div class="tpl-table-black-operation">
											已设为默认车辆
										</div>`
							}else{
							norelevance += `	
										<div class="tpl-table-black-operation">
											<a class='bind'>
												<i class="am-icon-pencil"></i> 关联
											</a>
										</div>`
							}
							norelevance += `	
									</td>
								</tr>`
						}
						$('.norelevance').find('tbody').html(norelevance)
						$('.bind').on('click',function(){
							var that=this
							tishi=layer.open({
								type: 1,
								title: false,
								
								id: "delete" //防止重复弹出
									,
								content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否关联</div>",
								btn: ['确定', '取消'],
								btnAlign: 'c' //按钮居中
									,
								shade: 0 //不显示遮罩
									,
								yes: function() {
									relearr=[];
									relearr.push($(that).closest('tr').data('carid'))
									proceedrelevance("{:input('param.driver_id')}",relearr);
									layer.close(tishi);
								},
								btn2: function() {
									layer.closeAll();
								}
							});
							
						})
					}else{
						Err(res.msg)
					}

				}
			});
		}
	}
	//请求关联车辆
	function requestrelevance() {
		var relevanceload = layer.load(0, {
			shade: [0.3, '#fff']
		});
		$.ajax({
			type: "post",
			url: "{:url('adminapi/driver/getUseCar')}",
			data: {
				driver_id: "{:input('param.driver_id')}",
				uid:"{$uid}"
			},
			success: function(res) {
				layer.close(relevanceload);
				console.log(res);
				var relevance = '';
				
				if(res.code == 1) {
					if(res.data.length > 0) {

						for(var i = 0; i < res.data.length; i++) {
							if(res.data[i].is_default){
								relevance += `<tr data-carid='${res.data[i].car_id}'>
								<td>${res.data[i].number}</td>
								<td>${res.data[i].car_type_name}</td>
								<td>
									<div class="tpl-table-black-operation">
										<a class='remove'>
											<i class="am-icon-pencil"></i>解除关联
										</a>
									</div>
								</td>
							</tr>`
							}else{
								relevance += `<tr data-carid='${res.data[i].car_id}'>
								<td>${res.data[i].number}</td>
								<td>${res.data[i].car_type_name}</td>
								<td>
									<div class="tpl-table-black-operation">
										<a class='remove'>
											<i class="am-icon-pencil"></i>解除关联
										</a>
										<a class='default'>
											<i class="am-icon-pencil"></i> 设为默认车辆
										</a>
									</div>
								</td>
							</tr>`
							}
							
						}
					} else {

						relevance += `<tr>
								<td>无关联车辆</td>
								<td>-</td>
								<td>
									-
								</td>
							</tr>`
					}
					$('.relevance').find('tbody').html(relevance);
					$('.remove').on('click', function() {
						var that=this
						tishi=layer.open({
								type: 1,
								title: false,
								
								id: "delete" //防止重复弹出
									,
								content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否解除关联</div>",
								btn: ['确定', '取消'],
								btnAlign: 'c' //按钮居中
									,
								shade: 0 //不显示遮罩
									,
								yes: function() {
									removerelearr = []
									removerelearr.push($(that).closest('tr').data('carid'))
									removerelevance("{:input('param.driver_id')}", removerelearr)
									layer.close(tishi);
								},
								btn2: function() {
									layer.closeAll();
								}
							});
						
					});
					$('.default').on('click',function(){
						var that=this
						tishi=layer.open({
								type: 1,
								title: false,
								//offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
									//,
								id: "delete" //防止重复弹出
									,
								content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否设为默认车辆</div>",
								btn: ['确定', '取消'],
								btnAlign: 'c' //按钮居中
									,
								shade: 0 //不显示遮罩
									,
								yes: function() {
									 isdefault("{:input('param.driver_id')}", $(that).closest('tr').data('carid'));
									 layer.close(tishi);
								},
								btn2: function() {
									layer.closeAll();
								}
							});
						
					})
					
				}else{
					Err(res.msg)
				}
			}
		});

	}

	//绑定车辆
	function proceedrelevance(driverid, caridarr) {
		console.log(caridarr)
		var proceedload = layer.load(0, {
			shade: [0.3, '#fff']
		});
		caridarr1 =JSON.stringify(caridarr)
			$.ajax({
				type: "post",
				url: "{:url('adminapi/driver/bindCar')}",
				data: {
					driver_id: driverid,
					car_id: caridarr1,
					uid:"{$uid}"
				},
				success: function(res) {
					layer.close(proceedload);
					console.log(res)
					if(res.code==1){
					Msg('绑定成功')
					requestnorelevance(1)
					requestrelevance()
					requestnorelevance(page)
					}else{
						Err(res.msg)
					}
				}
			})
	}
	//解除绑定车辆
	function removerelevance(driverid, caridarr) {
		var proceedload = layer.load(0, {
			shade: [0.3, '#fff']
		});
		caridarr1 =JSON.stringify(caridarr)
		$.ajax({
			type: "post",
			url: "{:url('adminapi/driver/delBindCar')}",
			data: {
				driver_id: driverid,
				car_id: caridarr1,
				uid:"{$uid}"
			},
			success: function(res) {
				layer.close(proceedload);
				console.log(res)
				if(res.code==1){
					Msg('解除成功')
					requestnorelevance(1)
					requestrelevance()
				}else{
					Err(res.msg)
				}
			}
		})
	}
	//设置默认车
	function isdefault(driverid, carid){
		var isdefaultload = layer.load(0, {
			shade: [0.3, '#fff']
		});
		$.ajax({
			type: "post",
			url: "{:url('adminapi/driver/setDefaultCar')}",
			data: {
				driver_id: driverid,
				car_id: carid,
				uid:"{$uid}"
			},
			success: function(res) {
				layer.close(isdefaultload);
				if(res.code==1){
					Msg('设置完成')
					requestrelevance();
					requestnorelevance(page)
				}else{
					Err(res.msg)
				}
			}
		})
	}
})